package com.adgox.tiantianbiting.home.programCustomBuy;

import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.ProgramChooseBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.ArrayList;

/**
 * Created by tang on 2018/12/29.
 */

public class ProgramCustomBuyPresenterImpl implements ProgramCustomBuyContract.Presenter {

    private ProgramCustomBuyContract.View mView;
    private int userId = ((UserInfo) SessionManager.getDefault().getUser()).getId();

    public ProgramCustomBuyPresenterImpl(ProgramCustomBuyContract.View mView) {
        this.mView = mView;
        mView.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void getProgramInfo(int programId) {
        OkGo.<BaseResponseBean<SubProgramBean>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_INFO + programId + "/" + userId)
                .execute(new JsonCallback<BaseResponseBean<SubProgramBean>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<SubProgramBean>> response) {
                        mView.onSetProgramInfo(response.body().getData());
                    }
                });
    }

    @Override
    public void getProgramSeasonList(int programId, int startPoint, int maxPage) {
        OkGo.<BaseResponseBean<PageBean<ProgramChooseBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_CHOOSE_INFO + programId)
                .params("sort", 0)
                .params("userId", userId)
                .params("startPoint", startPoint)
                .params("maxPage", maxPage)
                .execute(new JsonCallback<BaseResponseBean<PageBean<ProgramChooseBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<ProgramChooseBean>>> response) {
                        mView.onSetProgramSeasonList(response.body().getData());
                    }
                });
    }

    @Override
    public void buySelected(int programId, ArrayList<Long> buySeasons) {
        String url = ApiConstant.BASE_URL + ApiConstant.BUY_EVERY + "?userId=" + userId + "&programId=" + programId
                + "&seasonIds=";
        for (int i = 0; i < buySeasons.size(); i++) {
            if (i < buySeasons.size() - 1) {
                url = url + buySeasons.get(i) + ",";
            } else {
                url += buySeasons.get(i);
            }
        }
        OkGo.<BaseResponseBean<UserInfo>>get(url)
                .execute(new JsonCallback<BaseResponseBean<UserInfo>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<UserInfo>> response) {
                        if ("0".equals(response.body().getCode()) && response.body().getData() != null) {
                            if (response.body().getData().getDisPrice() >= 0) {
                                mView.onBuySuccess();
                            } else {
                                mView.onBuyFailed("余额不足，购买失败");
                            }
                        } else {
                            mView.onBuyFailed(response.body().getMessage());
                        }
                    }
                });

    }

}
